<?
/**
 * 애드페이 (AD-Pay for Gnuboard4)
 *
 * Copyright (c) 2010 Choi Jae-Young <www.miwit.com>
 *
 * 저작권 안내
 * - 저작권자는 이 프로그램을 사용하므로서 발생하는 모든 문제에 대하여 책임을 지지 않습니다. 
 * - 이 프로그램을 어떠한 형태로든 재배포 및 공개하는 것을 허락하지 않습니다.
 * - 이 저작권 표시사항을 저작권자를 제외한 그 누구도 수정할 수 없습니다.
 */

include_once("_common.php");
include_once("_config.php");

$g4[title] = "애드페이 주문관리";
include_once("adm-head.php");

$sql_common = " from $pb[order_table] ";
$sql_order = " order by  or_id desc ";
$sql_search = " where 1 ";

if ($pr_code) {
    $sql = "select pr_id from $pb[product_table] where pr_code = '$pr_code'";
    $row = sql_fetch($sql);
    $pr_id = $row[pr_id];
}

if ($sfl && $stx)
    $sql_search .= " and $sfl like '%$stx%' ";

if ($pr_code && $pr_id)
    $sql_search .= " and pr_id = '$pr_id' ";

switch ($status) {
    case '2' : $sql_search .= " and or_sdate > '$g4[time_ymd]' "; break;
    case '3' : $sql_search .= " and '$g4[time_ymd]' between or_sdate and or_edate and or_app = '1' "; break;
    case '4' : $sql_search .= " and or_edate < '$g4[time_ymd]' "; break;
}

$sql = "select * from $pb[product_table]";
$qry = sql_query($sql);
while ($row = sql_fetch_array($qry)) {
    $prd["$row[pr_id]"] = $row;
}

$sql = "select count(*) as cnt
        $sql_common
        $sql_search";
$row = sql_fetch($sql);
$total_count = $row[cnt];


$rows = 20;
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$sql = "select *
        $sql_common
        $sql_search
        $sql_order
        limit $from_record, $rows ";
$qry = sql_query($sql);

$list = array();
for ($i=0; $row = sql_fetch_array($qry); ++$i) {
    $mb = get_member($row[mb_id], "mb_id, mb_nick, mb_email, mb_homepage");
    $row[mb_name] = get_sideview($mb[mb_id], $mb[mb_nick], $mb[mb_email], $mb[mb_homepage]);
    $row[pr_name] = $prd["$row[pr_id]"][pr_name];
    $list[$i] = $row;
}

$list_count = count($list);

$write_pages = get_paging(10, $page, $total_page, "{$_SERVER[PHP_SELF]}?pr_code={$pr_code}&status=$status&{$qstr}&page=");

//if (!$sfl) $sfl = "mb_id";

$colspan = 10;

?>

<script type="text/javascript" src="<?=$g4[path]?>/js/sideview.js"></script>
<script type="text/javascript">
function open_stats(or_id) {
    win_stats = window.open('stats.php?or_id='+or_id, 'stats', 'width=800, height=600, scrollbars=yes');
    win_stats.focus();
}
function del(href) 
{
    if(confirm("한번 삭제한 자료는 복구할 방법이 없습니다.\n\n이미 차감되었던 사이버캐쉬(또는 포인트)도 복구됩니다.\n\n정말 삭제하시겠습니까?")) {
        if (g4_charset.toUpperCase() == 'EUC-KR') 
            document.location.href = href;
        else
            document.location.href = encodeURI(href);
    }
}
function ajax_app(or_id) {
    $.get("adm-order-app.php?or_id="+or_id);
}
function all_checked(sw) {
    var f = document.flist;

    for (var i=0; i<f.length; i++) {
        if (f.elements[i].name == "chk_or_id[]")
            f.elements[i].checked = sw;
    }
}

function check_confirm() {
    var f = document.flist;
    var chk_count = 0;

    for (var i=0; i<f.length; i++) {
        if (f.elements[i].name == "chk_or_id[]" && f.elements[i].checked)
            chk_count++;
    }

    if (!chk_count) {
        alert("게시물을 하나 이상 선택하세요.");
        return false;
    }
    return true;
}
function select_change_date() {
    var f = document.flist;

    if (!check_confirm())
        return;

    if (!confirm("정말 변경 하시겠습니까?"))
        return;

    f.action = "./adm-order-list-update.php";
    f.submit();
}
</script>
<?
//==============================================================================
// jquery date picker
//------------------------------------------------------------------------------
// 참고) ie 에서는 년, 월 select box 를 두번씩 클릭해야 하는 오류가 있습니다.
//------------------------------------------------------------------------------
// jquery-ui.css 의 테마를 변경해서 사용할 수 있습니다.
// base, black-tie, blitzer, cupertino, dark-hive, dot-luv, eggplant, excite-bike, flick, hot-sneaks, humanity, le-frog, mint-choc, overcast, pepper-grinder, redmond, smoothness, south-street, start, sunny, swanky-purse, trontastic, ui-darkness, ui-lightness, vader
// 아래 css 는 date picker 의 화면을 맞추는 코드입니다.
?>

<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/base/jquery-ui.css" rel="stylesheet" />
<style type="text/css">
<!--
.ui-datepicker { font:12px dotum; }
.ui-datepicker select.ui-datepicker-month, 
.ui-datepicker select.ui-datepicker-year { width: 70px;}
.ui-datepicker-trigger { margin:0 0 -5px 2px; }
-->
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
<script type="text/javascript">
/* Korean initialisation for the jQuery calendar extension. */
/* Written by DaeKwon Kang (ncrash.dk@gmail.com). */
jQuery(function($){
    $.datepicker.regional['ko'] = {
            closeText: '닫기',
            prevText: '이전달',
            nextText: '다음달',
            currentText: '오늘',
            monthNames: ['1월(JAN)','2월(FEB)','3월(MAR)','4월(APR)','5월(MAY)','6월(JUN)',
            '7월(JUL)','8월(AUG)','9월(SEP)','10월(OCT)','11월(NOV)','12월(DEC)'],
            monthNamesShort: ['1월','2월','3월','4월','5월','6월',
            '7월','8월','9월','10월','11월','12월'],
            dayNames: ['일','월','화','수','목','금','토'],
            dayNamesShort: ['일','월','화','수','목','금','토'],
            dayNamesMin: ['일','월','화','수','목','금','토'],
            weekHeader: 'Wk',
            dateFormat: 'yy-mm-dd',
            firstDay: 0,
            isRTL: false,
            showMonthAfterYear: true,
            yearSuffix: ''};
    $.datepicker.setDefaults($.datepicker.regional['ko']);

    $('#or_sdate').datepicker({
        showOn: 'button',
        buttonImage: '<?=$g4[path]?>/img/calendar.gif',
        buttonImageOnly: true,
        buttonText: "달력",
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        yearRange: 'c-99:c+99'
    }); 
    $('#or_edate').datepicker({
        showOn: 'button',
        buttonImage: '<?=$g4[path]?>/img/calendar.gif',
        buttonImageOnly: true,
        buttonText: "달력",
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        yearRange: 'c-99:c+99'
    }); 
});
</script>
<?
//==============================================================================
?>


<link rel="stylesheet" href="style.css" type="text/css"/>
<style type="text/css">
.tdb.btn a { font-size:11px; color:#888; }
</style>

<h3> <img src="img/miwit.gif" align="absmiddle"> <?=$g4[title]?> </h3>

<form name="fsearch" method="get" action="<?=$_SERVER[PHP_SELF]?>">
    총 <?=number_format($total_count)?> 개

    <select name="pr_code"> <!--onchange="location.href='<?=$_SERVER[PHP_SELF]?>?pr_code='+this.value;">-->
    <option value=""></option>
    <?
    $sql = "select * from $pb[product_table]";
    $qry = sql_query($sql);
    while ($row = sql_fetch_array($qry)) {
    ?>
    <option value="<?=$row[pr_code]?>"> <?=$row[pr_name]?> </option>
    <? } ?>
    </select>

    <select name="sfl">
        <option value="or_site"> 사이트이름 </option>
        <option value="mb_id"> 회원ID </option>
        <option value="or_url"> 사이트URL </option>
    </select>
    <select name="status">
        <option value="1"> 모두 </option>
        <option value="2"> 예약중 </option>
        <option value="3"> 노출중 </option>
        <option value="4"> 종료 </option>
    </select>
    <input type="text" size="20" name="stx" value="<?=$stx?>" class="ed">
    <input type="submit" value="검색" style="background-color:#efefef;">

    <script type="text/javascript">
    fsearch.pr_code.value = "<?=$pr_code?>";
    fsearch.sfl.value = "<?=$sfl?>";
    fsearch.status.value = "<?=$status?>";
    </script>
</form>

<form name="flist" method="post">
<input type="hidden" name="status" value="<?=$status?>">
<input type="hidden" name="pr_code" value="<?=$pr_code?>">
<input type="hidden" name="sfl" value="<?=$sfl?>">
<input type="hidden" name="stx" value="<?=$stx?>">
<input type="hidden" name="page" value="<?=$page?>">
<table class="content" width="100%" border="1" cellspacing="0" cellpadding="0" bordercolor="#cccccc">
<colgroup width="50"/>
<colgroup width="60"/>
<colgroup width="100"/>
<colgroup width=""/>
<colgroup width="40"/>
<colgroup width="160"/>
<!--<colgroup width="50"/>-->
<colgroup width="50"/>
<colgroup width="120"/>
<colgroup width="70"/>
<colgroup width="70"/>
<colgroup width="100"/>
<tr>
    <td class="tdh"> <input onclick="if (this.checked) all_checked(true); else all_checked(false);" type="checkbox"> </td>
    <td class="tdh"> 번호 </td>
    <td class="tdh"> 배너 </td>
    <td class="tdh"> 사이트 </td>
    <td class="tdh"> 승인 </td>
    <td class="tdh"> 기간 </td>
    <!--<td class="tdh"> 노출 </td>-->
    <td class="tdh"> 클릭 </td>
    <td class="tdh"> 회원 </td>
    <td class="tdh"> <?=$mw_cash[cf_cash_name]?> </td>
    <td class="tdh"> 포인트 </td>
    <td class="tdh"> <input type="button" value="등록" class="btn1" onclick="location.href='adm-order-form.php?pr_code=<?=$pr_code?>'"> </td>
</tr>
<? for ($i=0; $i<$list_count; $i++) { ?>
<tr>
    <td class="tdh"> <input type=checkbox name=chk_or_id[] value="<?=$list[$i][or_id]?>"> </td>
    <td class="tdb"> <?=$list[$i][or_id]?> </td>
    <td class="tdb"> <a href="adm-order.php?pr_code=<?=$prd[$list[$i][pr_id]][pr_code]?>"><?=$list[$i][pr_name]?></a> </td>
    <td class="tdb"> <?=$list[$i][or_site]?> </td>
    <td class="tdb"> <input type="checkbox" onclick="ajax_app(<?=$list[$i][or_id]?>)"<? if ($list[$i][or_app]) echo ' checked';?>> </td>
    <td class="tdb"> <?=$list[$i][or_sdate]?> ~ <?=$list[$i][or_edate]?> </td>
    <!--<td class="tdb"> <?=$list[$i][or_hit]?> </td>-->
    <td class="tdb"> <?=$list[$i][or_click]?> </td>
    <td class="tdb"> <?=$list[$i][mb_name]?></td>
    <td class="tdb"> <?=$list[$i][or_cash]?><?=$mw_cash[cf_cash_unit]?> </td>
    <td class="tdb"> <?=$list[$i][or_point]?>p </td>
    <td class="tdb btn">
        <a style="cursor:pointer;" onclick="open_stats(<?=$list[$i]['or_id']?>)">[통계]</a>
        <a href="adm-order-form.php?or_id=<?=$list[$i][or_id]?>&w=u&status=<?=$status?>&pr_code=<?=$pr_code?><?=$qstr?>">[수정]</a>
        <a href="javascript:del('adm-order-update.php?or_id=<?=$list[$i][or_id]?>&w=d&pr_code=<?=$pr_code?><?=$qstr?>')">[삭제]</a>
    </td>
</tr>
<? } ?> 
<?  if (!$i) { ?>
<tr>
    <td colspan="<?=$colspan?>" style="height:100px; text-align:center;"> 자료가 없습니다. </td>
</tr>
<? } ?>
</table>
<div style="margin:5px 0 0 0;">
시작일
<input type="text" class="ed" size="10" maxlength="10" name="or_sdate" id="or_sdate" value="<?=$order[or_sdate]?>" itemname="시작일" readonly>
~ 종료일
<input type="text" class="ed" size="10" maxlength="10" name="or_edate" id="or_edate" value="<?=$order[or_edate]?>" itemname="시작일" readonly>
<input type="button" value="변경" onclick="select_change_date()">
</div>
</form>

<p align="center"> <?=$write_pages?> </p>

<div style="height:50px;">&nbsp;</div>

<?
include_once("adm-tail.php");
?>
